#include "QShortcutProperty.h"
#include <QFileInfo>
#include <QAbstractItemDelegate>

#include "QShortcutPropertyEditor.h"

#include "private/QProperty_p.h"


using namespace QPE;

QShortcutProperty::QShortcutProperty(const QString& name)
	: QProperty(name, QVariant::String)
{

}

QShortcutProperty::~QShortcutProperty()
{
	//
}

QVariant QShortcutProperty::data (int column, int role) const
{
	if(column == DPC_Data && (Qt::DisplayRole == role || Qt::EditRole == role))
	{
		return d_ptr->VariantValue;
	}
	else
		return QProperty::data(column, role);
}


QWidget* QShortcutProperty::createEditor(QWidget * parent, const QStyleOptionViewItem& options, const QAbstractItemDelegate* delegate)
{
	Q_UNUSED(options);

	QShortcutEditWidget* tmpWidget = new QShortcutEditWidget(parent);
	if(delegate)
		QShortcutEditWidget::connect(tmpWidget, SIGNAL(commitData(QWidget*)), delegate, SIGNAL(commitData(QWidget*)));

	return tmpWidget;
	return NULL;
}


bool QShortcutProperty::setEditorData(QWidget* editor)
{
	QShortcutEditWidget* tmpWidget = qobject_cast<QShortcutEditWidget*>(editor);
	if(tmpWidget)
		tmpWidget->setShortcut(d_ptr->VariantValue.toString(), false);
	else
		return false;

	return true;
}


QVariant QShortcutProperty::getEditorData(QWidget* editor) const
{
	QShortcutEditWidget* tmpWidget = qobject_cast<QShortcutEditWidget*>(editor);
	if(tmpWidget)
		return tmpWidget->getShortcutAsString();

	return QVariant();
}


QString QShortcutProperty::type() const
{
	return "shortcut";
}

QProperty* QShortcutProperty::clone(bool include_children, QProperty* container) const
{
	return QProperty::clone(include_children, container ? container : new QShortcutProperty(getName()));
}

void QShortcutProperty::setValue(const QVariant &value)
{
	QProperty::setValue(QKeySequence::fromString(value.toString()).toString());
}

